import time
import random


balance = 50000
shopping_cart = {}
commodity_kind = {"电脑":5, "手机":4, "平板":3, "Switch":2, "PS5":1}
commodity_price = {"电脑":8000, "手机":3000, "平板":5000, "Switch":1800, "PS5":3000}
coupons = ['电脑 9 折券', "电脑 8 折券", "手机 9 折券", "手机 8 折券", "平板 9 折券", "平板 8 折券", "Switch 9 折券", "Switch 8 折券", "PS5 9 折券", "PS5 8 折券"]

print("正在为您随机抽取一张优惠券......")
coupon = random.choice(coupons)
info = coupon.split(' ')
discount_commodity = info[0]
discount_strength = info[1]
print("恭喜您抽到了" + coupon + "！本次购物过程中您购买" + discount_commodity + "时均可享受" + discount_strength + "折优惠！")
discount_strength = int(discount_strength)

while True:
    print("现有商品列表如下：")
    print("种类               数量               单价")
    for key, value in commodity_kind.items():
        print(key + "               " + str(value) + "               " + str(commodity_price[key]))
    commodity = input("输入你想买的商品：")
    if commodity == 'Q':
        print("为您打印购物小条：")
        t = time.strftime('%a %Y-%m-%d %H:%M:%S', time.localtime())
        print(t)
        print("种类               数量")
        total_price = 0
        for key, value in shopping_cart.items():
            print(key + "               " + str(value))
            total_price = total_price + commodity_price[key] * value
        print("购物总商品的价值为" + str(float(total_price)) + "元")
        print("您现在的账户余额为" + str(balance) + "元")
        print("感谢您的购物，再见！")
        break
    if commodity in commodity_kind:
        number = int(input("输入购买数量："))
        if number <= commodity_kind[commodity]:
            if commodity == discount_commodity:
                print("您正在使用折扣优惠！")
                if commodity_price[commodity] * number * (discount_strength / 10) <= balance:
                    shopping_cart[commodity] = number
                    commodity_kind[commodity] = commodity_kind[commodity] - number
                    balance = balance - commodity_price[commodity] * number * (discount_strength / 10)
                    print("添加购物车成功！")
                    print("此项预计花费" + str(commodity_price[commodity] * number * (discount_strength / 10)) + "元")
                else:
                    print("对不起，钱不够，请到其他商城购买！")
            else:
                if commodity_price[commodity] * number <= balance:
                    shopping_cart[commodity] = number
                    commodity_kind[commodity] = commodity_kind[commodity] - number
                    balance = balance - commodity_price[commodity] * number
                    print("添加购物车成功！")
                    print("此项预计花费" + str(float(commodity_price[commodity] * number)) + "元")
                else:
                    print("对不起，钱不够，请到其他商城购买！")
        else:
            print("商品库存数量不足，现在" + commodity + "的数量为" + str(commodity_kind[commodity]) + "。")
            continue
    else:
        print("对不起，没这个商品。别瞎乱下单！")
